package cn.mntool.backup.utils;

import cn.mntool.backup.config.BackupConfig;
import cn.mntool.backup.entity.BackupTimeProperties;
import cn.mntool.backup.entity.CommandProperties;
import org.apache.commons.lang3.SystemUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * describe:配置文件读取工具
 *
 * @author lianying
 * @date 2019/01/31
 */
public class PropertiesUtils {
    /**
     * log4j
     */
    private static final Logger logger= Logger.getLogger(PropertiesUtils.class);
    /**
     * 读取配置文件
     */
    private static Properties properties=new Properties();
    private static final String SETTINGS="sys.properties";
    static {
        try {
            properties.load(new FileInputStream(new File(PathUtils.classPath()+SETTINGS)));
        } catch (Exception e) {
            logger.info("读取配置信息异常",e);
        }
    }

    /**
     * 获得备份相关参数
     *
     * @return
     */
    public static BackupConfig getBackupConfig()
    {
        BackupConfig backupConfig=new BackupConfig();
        //设置数据库名
        backupConfig.setDbName(properties.getProperty("db.name"));
        //设置数据库用户名
        backupConfig.setDbUsername(properties.getProperty("db.username"));
        //设置数据库密码
        backupConfig.setDbPassword(properties.getProperty("db.password"));
        //设置数据库主机地址
        backupConfig.setDbHost(properties.getProperty("db.host"));
        //设置数据库端口
        backupConfig.setDbPort(Integer.valueOf(properties.getProperty("db.port")));
        //设置备份文件保存路径
        backupConfig.setDbSavePath(properties.getProperty("db.savePath"));
        //设置是否开启ftp上传
        backupConfig.setOpenFtp(Boolean.valueOf(properties.getProperty("openFtp")));
        //设置是否为sftp上传
        backupConfig.setSftp(Boolean.valueOf(properties.getProperty("isSftp")));
        //设置ftp主机地址
        backupConfig.setFtpHost(properties.getProperty("ftp.host"));
        //设置ftp端口
        backupConfig.setFtpPort(Integer.valueOf((String) properties.get("ftp.port")) );
        //设置ftp用户名
        backupConfig.setFtpUsername(properties.getProperty("ftp.username"));
        //设置是否为sftp上传
        backupConfig.setFtpPassword(properties.getProperty("ftp.password"));
        //设置是否为sftp上传
        backupConfig.setFtpSavePath(properties.getProperty("ftp.savePath"));
        return backupConfig;
    }
    /**
     * 获得备份命令参数
     *
     * @return
     */
    public static CommandProperties getCommandProperties()
    {
        CommandProperties commandProperties=new CommandProperties();
        //设置mysql命令
        if(SystemUtils.IS_OS_LINUX)
        {
            commandProperties.setMysqlCommand(properties.getProperty("linux.mysql"));
        }else if(SystemUtils.IS_OS_WINDOWS)
        {
            commandProperties.setMysqlCommand(properties.getProperty("windows.mysql"));
        }
        return commandProperties;
    }
    /**
     * 获得备份周期相关参数
     *
     * @return
     */
    public static BackupTimeProperties getBackupTimeProperties()
    {
        BackupTimeProperties backupTimeProperties=new BackupTimeProperties();
        backupTimeProperties.setCycle(Long.valueOf(properties.getProperty("db.cycle")));
        String [] times=properties.getProperty("db.range").split("-");
        backupTimeProperties.setBegin(Integer.valueOf(times[0]));
        backupTimeProperties.setEnd(Integer.valueOf(times[1]));
        return backupTimeProperties;
    }

    /**
     * 重新读取配置
     */
    public static void reload()
    {
        try {
            properties.load(new FileInputStream(new File(PathUtils.classPath()+SETTINGS)));
            logger.info("重新读取配置信息成功");
        } catch (Exception e) {
            logger.info("重新读取配置信息异常",e);
        }
    }

    /**
     * 通过key获得值
     *
     * @param key
     * @return
     */
    public static String get(String key)
    {
        return properties.getProperty(key);
    }

}
